package com.simple.trade.controller.vm;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.simple.common.entity.ResData;
import com.simple.trade.service.vm.CallbackHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 回调接收
 *
 * @author yasud
 * @date 2020年10月12日 09:19
 * @description
 */
@RestController
@RequestMapping("/receiver")
public class CallbackController {

    @Autowired
    CallbackHandler callbackHandler;


    /**
     * 这个作用于回调地址配置在渠道后台
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/fix/call/refund/{channelFlag}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String fixCallRefund(@PathVariable String channelFlag,
                                @MatrixVariable(pathVar = "channelFlag", required = false) Map<String, Object> param,
                                HttpServletRequest request) throws IOException {
        String res = callbackHandler.handleFix(request, channelFlag, true, param);
        return res;
    }

    /**
     * 这个作用于回调地址配置在渠道后台
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/fix/call/{channelFlag}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String fixCall(@PathVariable String channelFlag,
                          @MatrixVariable(pathVar = "channelFlag", required = false) Map<String, Object> param,
                          HttpServletRequest request) throws IOException {
        String res = callbackHandler.handleFix(request, channelFlag, false, param);
        return res;
    }

    /**
     * 专门为webhook 提供一个单独接口
     *
     * @return
     */
    @RequestMapping(value = "/fix/call/webhook/{channelFlag}",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public String webhook(@PathVariable String channelFlag,
                          @MatrixVariable(pathVar = "channelFlag", required = false) Map<String, Object> param,
                          HttpServletRequest request) throws IOException {
        String res = callbackHandler.handleWebhook(request, channelFlag, param);
        return res;
    }

    /**
     * 这个是对于自己传回调地址
     *
     * @return
     */
    @RequestMapping(value = "/dynamic/call/refund/{refundTradeInventory}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String dynamicCallRefund(@PathVariable Long refundTradeInventory,
                                    HttpServletRequest request) throws IOException {
        String res = callbackHandler.handleDynamic(request, null, refundTradeInventory, null);
        return res;
    }

    /**
     * 这个是对于自己传回调地址
     *
     * @return
     */
    @RequestMapping(value = "/dynamic/call/{tradeNo}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String dynamicCall(@PathVariable Long tradeNo,
                              @MatrixVariable(pathVar = "tradeNo", required = false) Map<String, Object> param,
                              HttpServletRequest request) throws IOException {
        String res = callbackHandler.handleDynamic(request, tradeNo, null, param);
        return res;
    }


    /**
     * 这个是对于自己传回调地址
     *
     * @return
     */
    @RequestMapping(value = "/dynamic/call/{flat}/{tradeNo}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String dynamicCall(@PathVariable Long tradeNo,
                              @PathVariable Boolean flat,
                              @MatrixVariable(pathVar = "tradeNo", required = false) Map<String, Object> param,
                              HttpServletRequest request) throws IOException {
        String res = callbackHandler.handleDynamic(request, tradeNo, flat, null, param);
        return res;
    }


    /**
     * 重新发送渠道回调
     *
     * @param notifyChannelId
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("/reSendNotify")
    public ResData reSendNotify(@RequestParam Long notifyChannelId) throws JsonProcessingException {
        callbackHandler.reSendNotify(notifyChannelId);
        return ResData.ok();
    }
}
